package com.example.demo.controller;

import com.example.demo.verify.JsonArrayValid;
import com.example.demo.verify.JsonObjectValid;
import com.example.demo.verify.XssValid;
import com.example.demo.common.constant.RuleCode;
import com.example.demo.entity.dto.StudentDTO;
import com.example.demo.common.Result;
import com.example.demo.verify.NoEmpty;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/student")
@Api(tags = "8、学生管理")
@ApiSupport(author = "罗海林", order = 8)
@Validated
@Slf4j
public class StudentController {

    @PostMapping("/getStudentist")
    @ApiOperation(value = "获取学生列表", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 1)
    public Result getStudentist(@RequestBody @Validated StudentDTO studentDTO) {
        Result res = new Result();
        res.ok();
        return res;
    }

    @PostMapping("/getStudentInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题"),
            @ApiImplicitParam(name = "phone", value = "手机号")
    })
    @ApiOperation(value = "获取学生信息", response = Result.class)
    @ApiOperationSupport(author = "陈艺文", order = 2)
    public Result getStudentInfo(@NoEmpty(message = "标题不能为空") @RequestParam String title,
                                 @NoEmpty(message = "手机号格式不正确") @RequestParam String phone) {
        Result res = new Result();
        res.ok();
        return res;
    }

    @PostMapping("/convertStudentInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "info", value = "JSON字段")
    })
    @ApiOperation(value = "转换学生信息", response = Result.class)
    @ApiOperationSupport(author = "伍莹莹", order = 3)
    public Result convertStudentInfo(
            @JsonObjectValid(
                    name = { "name", "mobile", "age" },
                    field = { "姓名", "手机号码", "年龄" },
                    rule = { RuleCode.noWick, RuleCode.mobile, RuleCode.num }
            ) @RequestParam String info
    ) {
        Result res = new Result();
        res.ok();
        return res;
    }

    @PostMapping("/convertStudentList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "list", value = "JSON数组")
    })
    @ApiOperation(value = "转换学生列表", response = Result.class)
    @ApiOperationSupport(author = "杨添以", order = 4)
    public Result convertStudentList(
            @JsonArrayValid(
                    name = { "name", "mobile", "age" },
                    field = { "姓名", "手机号码", "年龄" },
                    rule = { RuleCode.noWick, RuleCode.mobile, RuleCode.num }
            ) @RequestParam String list
    ) {
        Result res = new Result();
        res.ok();
        return res;
    }

    @PostMapping("/filterXSS")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题")
    })
    @ApiOperation(value = "过滤XSS攻击", response = Result.class)
    @ApiOperationSupport(author = "王彦远", order = 5)
    public Result filterXSS(@XssValid(message = "遭遇XSS与SQL注入攻击") @RequestParam String title) {
        Result res = new Result();
        res.ok();
        return res;
    }

    @PostMapping("/getIpAddress")
    @ApiOperation(value = "获取IP地址的所属区域", response = Result.class)
    @ApiOperationSupport(author = "陈艺文", order = 6)
    public Result getIpAddress(@RequestParam String ip) {
        String DB_PATH = "/file/resources/ip2region.xdb";
        String project_dir = System.getProperty("user.dir") +"";
        String dbPath = project_dir + "/" + DB_PATH;
        // 1、从 dbPath 加载整个 xdb 到内存。
        byte[] cBuff;
        try {
            cBuff = Searcher.loadContentFromFile(dbPath);
        } catch (Exception e) {
            return null;
        }

        // 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
        Searcher searcher;
        try {
            searcher = Searcher.newWithBuffer(cBuff);
        } catch (Exception e) {
            return null;
        }
        // 3、查询
        String region = null;
        try {
            region = searcher.search(ip);
        } catch (Exception e) {
            return null;
        }

        Result res = new Result();
        res.ok();
        res.put("address", region);
        return res;
    }

}
